﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CodeBuilder.Utility
{
    public static class Builder
    {
        public static string NameSpace { get; set; }
        public static string buildClass(string tableName, List<DataTableInfo> table)
        {
            List<Relation> relations = Database.GetRelation();

            //{1}
            StringBuilder defaultValue = new System.Text.StringBuilder();
            foreach (var item in table)
            {
                //处理主键，同时需要判断是否是自增，如果自增则不需要默认值
                if (item.IsPrimary && !item.IsIdentity)
                {
                    defaultValue.Append(string.Format("\tthis.{0}={1};", item.ColumnName, item.MappingResult.InitValue));
                    defaultValue.Append(System.Environment.NewLine);
                }
                else if (item.MappingResult.InitValue != null)
                {
                    defaultValue.Append(string.Format("\tthis.{0}={1};", item.ColumnName, item.MappingResult.InitValue));
                    defaultValue.Append(System.Environment.NewLine);
                }
            }

            StringBuilder items = new System.Text.StringBuilder();
            string keyType = "";
            foreach (var item in table)
            {
                items.Append("\t/// <summary>");
                items.Append(System.Environment.NewLine);
                items.Append(string.Format("\t///{0}", item.Description));
                items.Append(System.Environment.NewLine);
                items.Append("\t/// </summary>");
                items.Append(System.Environment.NewLine);

                #region 判断是否是主键，并且是多主键
                if (item.IsPrimary)
                {
                    keyType = item.MappingResult.ConvertType;
                }
                #endregion
                else
                {
                    if (!string.IsNullOrWhiteSpace(item.Description))
                    {
                        items.Append(string.Format("\t[Display(Name = \"{0}\")]", item.Description));
                        items.Append(System.Environment.NewLine);
                    }
                    items.Append(string.Format("\tpublic {0} {1} {{get;set;}}", item.MappingResult.ConvertType, item.ColumnName));
                    items.Append(System.Environment.NewLine);
                }
            }
            return string.Format(BuilderTemplate.BuildClassTemplate, NameSpace, tableName, keyType, defaultValue.ToString(), items.ToString());
        }

        public static string buildList(string tableName, List<DataTableInfo> table)
        {
            StringBuilder th = new StringBuilder();
            foreach (var item in table)
            {
                th.Append(string.Format("\t<th>{0}</th>", item.Description));
                th.Append(System.Environment.NewLine);
            }

            StringBuilder td = new StringBuilder();

            foreach (var item in table)
            {
                td.Append(string.Format("\t<td>@item.{0}</td>", item.ColumnName));
                td.Append(System.Environment.NewLine);
            }

            return string.Format(BuilderTemplate.BuildClassList, tableName, th, td);
        }
        public static string buildClassicEdit(string tableName, List<DataTableInfo> table)
        {
            StringBuilder items = new StringBuilder();
            foreach (var item in table)
            {
                items.Append(createElement(item));
                items.Append(System.Environment.NewLine);
            }

            return string.Format(BuilderTemplate.BuildClassicEdit, tableName, tableName, items.ToString());
        }

        public static string buildWindEdit(string tableName, List<DataTableInfo> table)
        {
            StringBuilder items = new StringBuilder();
            foreach (var item in table)
            {
                items.Append(createElement(item));
                items.Append(System.Environment.NewLine);
            }

            return string.Format(BuilderTemplate.BuildWindowTemplate, tableName, items.ToString());
        }

        private static string createElement(DataTableInfo tableInfo)
        {
            StringBuilder items = new StringBuilder();

            if (tableInfo.MappingResult.InputType == CodeBuilder.Utility.EDMMapping.InputType.CheckBox)
            {
                items.Append(string.Format("@Html.CheckBox(s => s.{0}", tableInfo.ColumnName));
                if (!tableInfo.IsNullable)
                {
                    items.Append(",new{@class=\"required\",@data_msg_required=\"请选择\"}");
                }
                items.Append(")");
            }
            else if (tableInfo.MappingResult.InputType == CodeBuilder.Utility.EDMMapping.InputType.TextArea)
            {
                items.Append(string.Format("@Html.TextArea(s => s.{0}", tableInfo.ColumnName));
                if (!tableInfo.IsNullable)
                {
                    items.Append(string.Format(",new{{@class=\"required\",@data_msg_required=\"请输入{0}\"}}", tableInfo.Description));
                }
                items.Append(")");
            }

            else if (tableInfo.MappingResult.InputType == CodeBuilder.Utility.EDMMapping.InputType.DateTime)
            {
                items.Append(string.Format("@Html.DatePicker(s => s.{0}", tableInfo.ColumnName));
                if (!tableInfo.IsNullable)
                {
                    items.Append(string.Format(",new{{@class=\"required\",@data_msg=\"请选择{0}\"}}", tableInfo.Description));
                }
                items.Append(")");
            }
            else
            {
                Dictionary<string, string> attr = new Dictionary<string, string>();
                items.Append(string.Format("@Html.TextBox(s => s.{0}", tableInfo.ColumnName));
                if (!tableInfo.IsNullable)
                {
                    attr.Add("required", "@data_msg=\"请输入" + tableInfo.Description + "\"");
                    if (tableInfo.MappingResult.InputType == CodeBuilder.Utility.EDMMapping.InputType.Number)
                    {
                        attr.Add("number", "@data_msg_number=\"请输入数字\"");
                    }

                    if (tableInfo.MappingResult.InputType == CodeBuilder.Utility.EDMMapping.InputType.Digits)
                    {
                        attr.Add("digits", "@data_msg_number=\"请输入整数\"");
                    }
                }
                string @class = "";
                string message = "";
                if (attr.Count > 0)
                {
                    foreach (var item in attr)
                    {
                        @class += item.Key + " ";
                        message += item.Value + ",";
                    }
                }

                if (attr.Count > 0)
                {
                    @class = @class.Trim();
                    message = message.Substring(0, message.Length - 1);
                    items.Append(string.Format(",new{{@class=\"{0}\",{1}}}", @class, message));
                }

                items.Append(")");
            }

            return (string.Format("\t<tr><td>{0}</td><td>{1}</td></tr>", tableInfo.Description, items.ToString()));
        }
    }
}
